package TestDemo.example.service.impl;

import TestDemo.example.mapper.StudentMapper;
import TestDemo.example.pojo.ClazzOption;
import TestDemo.example.pojo.PageResult;
import TestDemo.example.pojo.Student;
import TestDemo.example.pojo.StudentQueryParam;
import TestDemo.example.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;

@Service
public class StudentServiecImpl implements StudentService {
    private static final Logger log = LoggerFactory.getLogger(StudentServiecImpl.class);
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult getStudentParamList(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        Page<Student> studentPage = (Page<Student>) studentMapper.getStudentParamList(studentQueryParam);
        return new PageResult(studentPage.getTotal(), studentPage.getResult());
    }

    @Override
    public void deleteById(ArrayList<Integer> ids) {
        studentMapper.deleteById(ids);
    }

    @Override
    public void add(Student student) {

        log.info("新增员工{}", student.getUpdateTime());
        studentMapper.add(student);
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
    }

    @Override
    public void updateViolation(Integer id, Integer score) {
        log.info("updateViolation{}", id);
        studentMapper.updateViolation(id, score);
    }

    @Override
    public ArrayList<Map<String, Object>> getStudentDegreeData() {
       log.info("查询员工性别数据");
       ArrayList<Map<String, Object>> studentDegreeData = studentMapper.getStudentDegreeData();
       return studentMapper.getStudentDegreeData();
    }

    @Override
    public ClazzOption getStudentCount() {
        log.info("查询员工性别数据");
        ArrayList<Map<String, Object>> studentCountData = studentMapper.getStudentCountData();
        ArrayList<String> clazzList = new ArrayList();
        ArrayList<Object> datalist=new ArrayList();
        for (Map<String, Object> map : studentCountData){
            String name = (String)map.get("cname");
            log.info("******{}", name);
            Object value = map.get("value");
            clazzList.add(name);
            datalist.add(value);
        }

        return new ClazzOption(clazzList, datalist);
    }
}
